/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Bean.ApplicantBean;
import Bean.SchedulingInterviewBean;
import Utility.DBProcess;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Vietlt
 */
public class SchedulingInterviewDAO {

    public static void main(String[] args) {
        /*
         List<SchedulingInterviewBean> beans=new SchedulingInterviewDAO().LoadAllSchedulingInterview();
         for (SchedulingInterviewBean d : beans) {
         System.out.println("no co chi");
         System.out.println("EmployeeID:"+d.getEmployeeID());
         System.out.println("ApplicationID:"+d.getApplicantID());
         System.out.println("ApplicationID:"+d.getStatus());
         System.out.println("ApplicationID:"+d.getTime());
         }
         System.out.println("----load by ID-------");
         SchedulingInterviewBean bean=new SchedulingInterviewDAO().LoadAllSchedulingInterview(1);
         System.out.println("sad:"+bean.getApplicantID());
         System.out.println("sad:"+bean.getStatus());
         System.out.println("sad:"+bean.getTime());*/
        int n=new SchedulingInterviewDAO().DeleteSchedulingInterview(1);
        if(n==1)
        {
            System.out.println("okie");
        }

    }

    public List<SchedulingInterviewBean> LoadAllSchedulingInterview() {
        List<SchedulingInterviewBean> list = new ArrayList<SchedulingInterviewBean>();
        Connection conn = null;
        CallableStatement stm = null;
        ResultSet rs = null;
        conn = DBProcess.getConnection();
        try {
            stm = conn.prepareCall("{ call SchedulingInterview_getall}");
            rs = stm.executeQuery();
            while (rs.next()) {
                SchedulingInterviewBean Sc = new SchedulingInterviewBean();
                Sc.setEmployeeID(rs.getInt("EmployeeID"));
                Sc.setApplicantID(rs.getInt("ApplicantID"));
                Sc.setStatus(rs.getInt("Status"));
                Sc.setTime(rs.getString("Time"));
                list.add(Sc);
                if (list.isEmpty()) {
                    String err = "List empty !!!";
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ApplicantDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBProcess.closeResultSet(rs);
            DBProcess.closeStatement(stm);
            DBProcess.closeConnection(conn);
        }
        return list;
    }

    public SchedulingInterviewBean LoadAllSchedulingInterview(int EmployeeID) {
        Connection conn = null;
        CallableStatement stm = null;
        ResultSet rs = null;
        conn = DBProcess.getConnection();
        SchedulingInterviewBean sc = new SchedulingInterviewBean();
        try {
            stm = conn.prepareCall("{ call SchedulingInterview_get (?)}");
            stm.setInt(1, EmployeeID);
            rs = stm.executeQuery();
            while (rs.next()) {
                sc.setEmployeeID(rs.getInt("EmployeeID"));
                sc.setApplicantID(rs.getInt("ApplicantID"));
                sc.setStatus(rs.getInt("Status"));
                sc.setTime(rs.getString("Time"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ApplicantDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBProcess.closeResultSet(rs);
            DBProcess.closeStatement(stm);
            DBProcess.closeConnection(conn);
        }
        return sc;
    }

    public int AddNewSchedulingInterview(SchedulingInterviewBean sc) {
        SchedulingInterviewBean sc1 = null;
        Connection conn = null;
        CallableStatement stm = null;
        conn = DBProcess.getConnection();
        int isComplete = 0;

        try {
            String dob = sc.getTime();
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            Date dd = format.parse(dob);
            stm = conn.prepareCall("{ call SchedulingInterview_add(?,?,?,?)}");
            stm.setInt(1, sc.getEmployeeID());
            stm.setInt(2, sc.getApplicantID());
            stm.setInt(3, sc.getStatus());
            stm.setDate(4, new java.sql.Date(dd.getTime()));

            if (stm.executeUpdate() > 0) {
                isComplete = 1;
            } else {
                isComplete = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return isComplete;
    }
    public int UpdateSchedulingInterview(SchedulingInterviewBean sc) {
        SchedulingInterviewBean sc1 = null;
        Connection conn = null;
        CallableStatement stm = null;
        conn = DBProcess.getConnection();
        int isComplete = 0;

        try {
            String dob = sc.getTime();
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            Date dd = format.parse(dob);
            stm = conn.prepareCall("{ call SchedulingInterview_add(?,?,?,?)}");
            stm.setInt(1, sc.getEmployeeID());
            stm.setInt(2, sc.getApplicantID());
            stm.setInt(3, sc.getStatus());
            stm.setDate(4, new java.sql.Date(dd.getTime()));

            if (stm.executeUpdate() > 0) {
                isComplete = 1;
            } else {
                isComplete = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return isComplete;
    }
    public int DeleteSchedulingInterview(int EmployeeID) {
        SchedulingInterviewBean sc1 = null;
        Connection conn = null;
        CallableStatement stm = null;
        conn = DBProcess.getConnection();
        int isComplete = 0;

        try {
            stm = conn.prepareCall("{ call SchedulingInterview_delete(?)}");
            stm.setInt(1, EmployeeID);
            if (stm.executeUpdate() > 0) {
                isComplete = 1;
            } else {
                isComplete = 0;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return isComplete;
    }
    
}
